//
//  SNMyTabeleView.m
//  project
//
//  Created by 黄建华 on 2018/8/27.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "SNMyTabeleView.h"

@implementation SNMyTabeleView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-  (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView
{
    _myTableView                             = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height) style:UITableViewStylePlain];
    _myTableView.delegate                     = self;
    _myTableView.dataSource                   = self;
    _myTableView.separatorStyle               = UITableViewCellSeparatorStyleNone;
    _myTableView.showsVerticalScrollIndicator = NO;
    [self addSubview:_myTableView];
    
   
}

- (void)createMjFooter{
    _footer                            = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadFooterData)];
    _footer.backgroundColor            = SNCOLORGRAYLINE;
    _footer.alpha                      = 0.5;
    _footer.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    [_footer setTitle:SN_Text(@"点击或者上拉加载更多") forState:MJRefreshStateIdle];
    [_footer setTitle:SN_Text(@"松开刷新") forState:MJRefreshStatePulling];
    [_footer setTitle:SN_Text(@"加载 ...") forState:MJRefreshStateRefreshing];
    _footer.stateLabel.font                       = [UIFont systemFontOfSize:15];
    _footer.stateLabel.textColor                  = SNCOLORGRAY;
    _myTableView.mj_footer                          = _footer;
    _myTableView.mj_footer.automaticallyChangeAlpha = YES;
    _myTableView.mj_footer                          = _footer;
}

- (void)createMjHeader
{
    _myHeader = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadHeaderData)];
    _myHeader.backgroundColor            = [UIColor colorFromHex:0x4b4b4b];
    _myHeader.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    // 设置文字
    [_myHeader setTitle:SN_Text(@"下拉刷新") forState:MJRefreshStateIdle];
    [_myHeader setTitle:SN_Text(@"松开刷新") forState:MJRefreshStatePulling];
    [_myHeader setTitle:SN_Text(@"加载 ...") forState:MJRefreshStateRefreshing];
    
    // 设置字体
    _myHeader.stateLabel.font           = [UIFont systemFontOfSize:15];
    _myHeader.lastUpdatedTimeLabel.font = [UIFont systemFontOfSize:14];
    _myHeader.lastUpdatedTimeLabel.text = [NSString stringWithFormat:@"%@:%@", SN_Text(@"上次更新"), [[NSDate date]dateToString]];
    
    // 设置颜色
    _myHeader.stateLabel.textColor           = SNCOLORGRAY;
    _myHeader.lastUpdatedTimeLabel.textColor = SNCOLORGRAY;
    // 设置刷新控件
    _myTableView.mj_header                          = _myHeader;
    _myTableView.mj_header.automaticallyChangeAlpha = YES;
}

- (void)loadHeaderData {
    [self endRefreshing:3.0 withTypte:1];
}

- (void)loadFooterData
{
    [self endRefreshing:3.0 withTypte:2];
}

- (void)endRefreshing:(CGFloat)time withTypte:(NSInteger)type {
    __unsafe_unretained UITableView *tableView = self.myTableView;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 结束刷新
        if (type == 1) {
            [tableView.mj_header endRefreshing];
        }
        else
        {
            [tableView.mj_footer endRefreshing];
        }
    });
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString     *identifier = @"systemmessageTableViewCell";
    UITableViewCell *cell       = [tableView cellForRowAtIndexPath:indexPath];
    
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }

    return cell;
}

@end
